set more off
clear all

global base "X:\Dropbox\educacion superior 2021\submission\EDCC\For publication\Replication package\Data"
cd "$base"

***** RONDA 1 *****

use "input/peru_r1/pesubsec2householdroster8.dta"

gen parent=(RELATE==1)
gen msib_r1=((RELATE==5|RELATE==10|RELATE==11)&SEX==1)
gen msib_older=((RELATE==5|RELATE==10|RELATE==11)&SEX==1&9<=AGE)
gen fsib_r1=((RELATE==5|RELATE==10|RELATE==11)&SEX==2)
gen fsib_older=((RELATE==5|RELATE==10|RELATE==11)&SEX==2&9<=AGE)
gen mom=1 if RELATE==1&SEX==2
gen teenagemom=0 if mom==1
replace teenagemom=1 if mom==1&AGE<=27

collapse (sum) msib_r1 fsib_r1 msib_older fsib_older parent mom teenagemom, by(CHILDID)
gen parents=(parent==2)
recode parents (0=1) (1=0)
replace teenagemom=. if mom==0
gen olderm=msib_older!=0
gen olderf=fsib_older!=0

merge 1:1 CHILDID using "input/peru_r1/pechildlevel8yrold"
drop _merge

ren (CAREID wi SEEDAD HHSIZE CHDBORN ORDER CHDALIVE MOTHIDIO SEX zhfa zwfa ///
	WANTCLD ASPECTO1 ASPECTO2 ASPECTO3 ASPECTO4 ASPECTO5 PARTICI1 PARTICI2 ///
	PARTICI3 PARTICI4 PARTICI5 PARTICI6 CHORES TMCHORE TRUST CHLDWORK agechild ///
	LEVLWRIT NUMERACY levlread ravens ASISTCEI DONDEN SCHSTART SCHTYP SCHIGH RESPECT) ///
	(careid_r1 wi_r1 seedad_r1 hhsize_r1 chborn_r1 order siblings_r1 indigena_r1 sex zhfa_r1 zwfa_r1 ///
	wantcld aspecto1_r1 aspecto2_r1 aspecto3_r1 aspecto4_r1 aspecto5_r1 partici1_r1 partici2_r1 ///
	partici3_r1 partici4_r1 partici5_r1 partici6_r1 chores_r1 timechore_r1 trust_r1 chwork_r1 agechild_r1 ///
	write_r1 numeracy_r1 read_r1 raven_r1 preschool migrated_r1 scstart_r1 sctype_r1 grado_r1 respect_r1)
	
gen health_r1=(HPFRIEND==1|HPWORK==1)

gen costa_r1=(REGION==31)
gen sierra_r1=(REGION==32)
gen selva_r1=(REGION==33)

gen rural_r1=(TYPESITE==2)
gen ob_r1= (zwfa_r1>2 & zwfa_r1<=5)

recode sctype_r1 (88 99 = .) (1 = 0) (2 = 1)
recode indigena_r1 (1=0) (2 3 4=1) (8 77 88=.)
recode wantcld chborn_r1 order siblings_r1 (77 99 = .)
recode trust_r1 timechore_r1(99=.)
recode chwork_r1 (77=.)
recode write_r1 (77 99=.) (3=2) (2=3) 
recode numeracy_r1 respect_r1 (77 99=.) (2=0)
recode read_r1 (77 99=.)
recode preschool (2=0)
recode seedad_r1 (88 99=.)
recode chores_r1 chwork_r1 sex wantcld (2=0)
recode grado_r1 (88=.)
recode raven_r1 (88=.)

recode migrated_r1 (77=0) (1=0) 
replace migrated_r1=1 if migrated_r1!=0&migrated_r1!=.

recode partici1_r1 partici2_r1 partici3_r1 partici4_r1 partici5_r1 partici6_r1 (3 88=.) (2=0)
egen partici_r1=rowmean(partici1_r1 partici2_r1 partici3_r1 partici4_r1 partici5_r1 partici6_r1)
egen n_partici_r1=rownonmiss(partici1_r1 partici2_r1 partici3_r1 partici4_r1 partici5_r1 partici6_r1)
replace partici_r1=. if n_partici_r1<=2
drop n_partici_r1

recode aspecto1_r1 aspecto2_r1 aspecto3_r1 aspecto4_r1 aspecto5_r1 (88 99=.) (1=3) (3=2) (2=1)
egen schoolq_r1=rowmean(aspecto1_r1 aspecto2_r1 aspecto3_r1 aspecto4_r1 aspecto5_r1)

gen partnerdrunk=(EMBORR==1) if PETPAR == 1

merge 1:m CHILDID using "input/peru_r1/pesubsec2householdroster8.dta"
drop if ID!=careid_r1
drop _merge
ren YRSCHOOL careed_r1
recode careed_r1 (0 31 32 = 0) (33 = 1) (34 35 37 = 2) (36 38 = 3) (88 99 = .)

gen CHILDCODE=substr(CHILDID,3,.)
destring CHILDCODE, replace

* Standardizing

foreach v of varlist write_r1 numeracy_r1 read_r1 raven_r1 chwork_r1 chores_r1 seedad_r1{
egen mean_`v' = mean(`v')
egen sd_`v'=sd(`v')
gen z_`v'= (`v' - mean_`v')/sd_`v'
drop `v'
rename z_`v' `v'
}

keep CLUSTID CHILDID CHILDCODE costa_r1 sierra_r1 selva_r1 rural_r1 wi_r1 seedad_r1 hhsize_r1 sctype_r1 ///
msib_r1 fsib_r1 msib_older fsib_older olderm olderf parents order siblings_r1 indigena_r1 sex zhfa_r1 zwfa_r1 ob_r1 wantcld partici_r1 partici1_r1 partici2_r1 partici3_r1 partici4_r1 partici5_r1 partici6_r1 partnerdrunk ///
chores_r1 timechore_r1 trust_r1 chwork_r1 agechild_r1 write_r1 numeracy_r1 read_r1 raven_r1 preschool migrated_r1 careed_r1 scstart_r1 careed_r1 grado_r1 health_r1 teenagemom respect_r1

save "working/r1_intermedia.dta", replace

***** RONDA 2 *****

use "input/peru_r2/pesubhouseholdmember12.dta", clear

keep if (RELATE==7|RELATE==8|RELATE==9|RELATE==10)&LIVHSE!=3

gen msib_r2=(MEMSEX==1)&LIVHSE==1
gen fsib_r2=(MEMSEX==2)&LIVHSE==1

gen msib_all_r2=(MEMSEX==1)
gen fsib_all_r2=(MEMSEX==2)

collapse (sum) msib_r2 fsib_r2 msib_all_r2 fsib_all_r2, by(CHILDID)

merge 1:1 CHILDID using "input/peru_r2/pechildlevel12yrold.dta"
drop _merge

ren (SEEDAD agechild HHSIZE BORNBEF BORNAFT ANYHELP zhfa zwfa bmi wi CAREED LONGTERM ///
GUCLOSE GUFINAN GUHELPS GUFINYOU GUCARES GUOLD GUEMOT EXPEARN EXPEDU EXPIND EXPLEAV EXPMAR EXPCHILD ACCESS05) ///
(seedad_r2 agechild_r2 hhsize_r2 bornbef_r2 bornaft_r2 helphh_r2 zhfa_r2 zwfa_r2 bmi_r2 wi_r2 careed_r2 health_r2 ///
gu_liveclose gu_financesiblings gu_helphousework gu_financeparents gu_caresiblings gu_careparents gu_emotsupport ///
age_earnmoney age_leaveeduc age_financindep age_leavehh age_married age_child credit_r2) ///

gen rural_r2=(TYPESITE==2)
replace rural_r2=. if TYPESITE==.

gen costa_r2=(REGION==31)
gen sierra_r2=(REGION==32)
gen selva_r2=(REGION==33)
recode costa_r2 sierra_r2 selva_r2 (88=.)

gen ob_r2= (zwfa_r2>2 & zwfa_r2<=5)

recode careed_r2 (0 1 2 3 4 5 = 0) (6 7 8 9 10 = 1) (11 13 15 = 2) (14 16 = 3) (18 . = .)
recode seedad_r2 (7=3) (88=.)

merge 1:1 CHILDID using "input/peru_r2/pechildquest12yrold.dta"

ren (CEXPEARN CEXPEDU CEXPIND CEXPLEAV CEXPMAR) (age_earnmoney_child age_leaveeduc_child age_financindep_child age_leavehh_child age_married_child)

recode age_earnmoney age_leaveeduc age_financindep age_leavehh age_married age_child ///
	age_earnmoney_child age_leaveeduc_child age_financindep_child age_leavehh_child age_married_child ///
	(77 79 99 = .)

ren CGRDLIKE exp_r2
recode exp_r2 (2 6 10 11 13 15 = 0) (14 16 = 1) (18 79 88 99 = .)

ren SCTYPE sctype_r2
recode sctype_r2 (88 = .) (1 7 = 1) (4 = 0)

ren GRADNOW grado_r2
recode grado_r2 (88=.)

ren MISSCH falta_r2
recode falta_r2 (88=.)

drop _merge

* Expectations of future contribution (+)

recode gu_liveclose gu_financesiblings gu_helphousework gu_financeparents gu_caresiblings gu_careparents gu_emotsupport (77 79 88 = .)
egen guexpectations=rowmean(gu_liveclose gu_financesiblings gu_helphousework gu_financeparents gu_caresiblings gu_careparents gu_emotsupport)
egen n_guexpectations=rownonmiss(gu_liveclose gu_financesiblings gu_helphousework gu_financeparents gu_caresiblings gu_careparents gu_emotsupport)
replace guexpectations=. if guexpectations<=2
drop n_guexpectations

* Relationship with parents (+)

ren (FAIRPUN SISLESS BROLESS BROFREE SISFREE ) (par1_r2 par2_r2 par3_r2 par4_r2 par5_r2)

recode par1_r2 (79 88 99 = .) (1 = 2) (2 = 1)
recode par2_r2 par3_r2 par4_r2 par5_r2 (79 88 99 = .) (2 = 1) (1 = 0) (0 = 2)
egen relpar_r2=rowmean(par1_r2 par2_r2 par3_r2 par4_r2 par5_r2)
egen n_relpar=rownonmiss(par1_r2 par2_r2 par3_r2 par4_r2 par5_r2)
replace relpar_r2=. if n_relpar<=2
drop n_relpar 

* Use of time

foreach var in CCHCARE CHHCHORE CNPAYWRK CPAYWORK CSCHOOL CSTUDY CPLAY{
	replace `var'=. if `var'==88
	replace `var'=`var'/15
}

recode CHLDWORK (88 = .)

ren (CCHCARE CHHCHORE CNPAYWRK CPAYWORK CSCHOOL CSTUDY CPLAY CHLDWORK ENRSCH /*SCHOOLEN*/) (timecare_r2 timechore_r2 timepwork_r2 timenpwork_r2 timeschool_r2 timestudy_r2 timeleisure_r2 chwork_r2 enrschool_r2 /*schoolq_r2*/)

* Expenditure

ren (SPYR03 SPYR04 SPYR11A SPYR12A SPYR13A SPYR15 SPYR16) (exp_cloth1_r2 exp_cloth2_r2 exp_school1_r2 exp_school2_r2 exp_school3_r2 exp_school4_r2 exp_school5_r2)
recode exp_cloth1_r2 exp_cloth2_r2 exp_school1_r2 exp_school2_r2 exp_school3_r2 exp_school4_r2 exp_school5_r2 (-88 -99 = .)

gen sib_r2=msib_r2+fsib_r2
replace sib_r2=0 if sib_r2==.
gen children_r2=sib_r2+1

egen exp_cloth_r2=rowtotal(exp_cloth1_r2 exp_cloth2_r2)
replace exp_cloth_r2=exp_cloth_r2/children_r2
replace exp_cloth_r2=. if Situac_R2!=1
egen exp_school_r2=rowtotal(exp_school1_r2 exp_school2_r2 exp_school3_r2 exp_school4_r2 exp_school5_r2)
replace exp_school_r2=exp_school_r2/children_r2
replace exp_school_r2=. if Situac_R2!=1

*** Skills

* Cognitive
gen ppvt_spanish_r2= (language1==1)
gen score_ppvt_r2=score_ppvt
gen score_math_r2=score_math
gen rscore_ppvt_r2=rscorelang_ppvt
gen rscore_math_r2=rscorelang_math
gen write_r2=LEVLWRIT

* some fixes
replace rscore_math_r2 = . if rscore_math_r2 == 0
recode write_r2 rscore_ppvt_r2 (79 88 = .)

* Trust index

foreach x in TR1 TR2 TR3 TR4{
recode `x' (77 79 88 99 = .)
gen `x'_1=.
replace `x'_1=1 if `x'==0 | `x'==3
replace `x'_1=2 if `x'==2
replace `x'_1=3 if `x'==1
replace `x'=`x'_1
drop `x'_1
}

ren (TR1 TR2 TR3 TR4) (tr1_r2 tr2_r2 tr3_r2 tr4_r2)

* Self-respect index

foreach x in SD1 SD3 {
recode `x' (77 79 88 99 = .)
gen `x'_1=.
replace `x'_1=1 if `x'==0
replace `x'_1=2 if `x'==2
replace `x'_1=3 if `x'==1
drop `x'
rename `x'_1 `x'
}

foreach x in SD2 SD4 SD5{
recode `x' (77 79 88 99 = .)
gen `x'_inv=.
replace `x'_inv=1 if `x'==1
replace `x'_inv=2 if `x'==2
replace `x'_inv=3 if `x'==0
drop `x'
rename `x'_inv `x'
}

ren (SD1 SD2 SD3 SD4 SD5) (sd1_r2 sd2_r2 sd3_r2 sd4_r2 sd5_r2)

* Pride index

foreach x in PS1 PS3 PS5 PS9{
recode `x' (77 79 88 99 = .)
gen `x'_1=.
replace `x'_1=1 if `x'==0
replace `x'_1=2 if `x'==2
replace `x'_1=3 if `x'==1
drop `x'
rename `x'_1 `x'
}


foreach x in PS2 PS4 PS6 PS7 PS8{
recode `x' (77 79 88 99 = .)
gen `x'_inv=.
replace `x'_inv=1 if `x'==1
replace `x'_inv=2 if `x'==2
replace `x'_inv=3 if `x'==0
drop `x'
rename `x'_inv `x'
}

ren (PS1 PS2 PS3 PS4 PS5 PS6 PS7 PS8 PS9) (ps1_r2 ps2_r2 ps3_r2 ps4_r2 ps5_r2 ps6_r2 ps7_r2 ps8_r2 ps9_r2)

* Agency index

foreach x in AG1 AG3 AG4{
recode `x' (77 79 88 99 = .)
gen `x'_1=.
replace `x'_1=1 if `x'==0
replace `x'_1=2 if `x'==2
replace `x'_1=3 if `x'==1
drop `x'
rename `x'_1 `x'
}


foreach x in AG2 AG5{
recode `x' (77 79 88 99 = .)
gen `x'_inv=.
replace `x'_inv=1 if `x'==1
replace `x'_inv=2 if `x'==2
replace `x'_inv=3 if `x'==0
drop `x'
rename `x'_inv `x'
}


ren (AG1 AG2 AG3 AG4 AG5) (ag1_r2 ag2_r2 ag3_r2 ag4_r2 ag5_r2)

* Standardizing

egen total_r2_tr=rowtotal(tr1_r2 tr2_r2 tr3_r2 tr4_r2)
egen nonmiss_r2_tr=rownonmiss(tr1_r2 tr2_r2 tr3_r2 tr4_r2)
gen trust_index_r2=total_r2_tr/nonmiss_r2_tr

egen total_r2_ps=rowtotal(ps1_r2 ps2_r2 ps3_r2 ps4_r2 ps5_r2 ps6_r2 ps7_r2 ps8_r2 ps9_r2)
egen nonmiss_r2_ps=rownonmiss(ps1_r2 ps2_r2 ps3_r2 ps4_r2 ps5_r2 ps6_r2 ps7_r2 ps8_r2 ps9_r2)
gen pride_index_r2=total_r2_ps/nonmiss_r2_ps

egen total_r2_ag=rowtotal(ag1_r2 ag2_r2 ag3_r2 ag4_r2 ag5_r2)
egen nonmiss_r2_ag=rownonmiss(ag1_r2 ag2_r2 ag3_r2 ag4_r2 ag5_r2)
gen agency_index_r2=total_r2_ag/nonmiss_r2_ag

egen total_r2_sd=rowtotal(sd1_r2 sd2_r2 sd3_r2 sd4_r2 sd5_r2)
egen nonmiss_r2_sd=rownonmiss(sd1_r2 sd2_r2 sd3_r2 sd4_r2 sd5_r2)
gen respect_index_r2=total_r2_sd/nonmiss_r2_sd

foreach v of varlist rscore_ppvt_r2 rscore_math_r2 score_ppvt_r2 score_math_r2 write_r2 trust_index_r2 pride_index_r2 agency_index_r2 respect_index_r2 relpar_r2 exp_cloth_r2 exp_school_r2 timestudy_r2 timechore_r2 chwork_r2 {
egen mean_`v' = mean(`v')
egen sd_`v'=sd(`v')
gen z_`v'= (`v' - mean_`v')/sd_`v'
drop `v'
rename z_`v' `v'
}

gen CHILDCODE=substr(CHILDID,3,.)
destring CHILDCODE, replace

foreach var of varlist age_* {
	recode `var' (0 = .)
}

*** Expectations

gen expect_highered = GRADLIKE == 14 | GRADLIKE == 16 & EXPGRADE == 1 if !missing(GRADLIKE) 

***

keep CHILDID CHILDCODE chwork_r2 seedad_r2 agechild_r2 hhsize_r2 health_r2 costa_r2 sierra_r2 selva_r2 ///
rural_r2 bornbef_r2 bornaft_r2 helphh_r2 zhfa_r2 zwfa_r2 bmi_r2 ob_r2 wi_r2 careed_r2 sctype_r2 /*schoolq_r2*/ ///
sib_r2 msib_r2 msib_all_r2 fsib_r2 fsib_all_r2 ///
par1_r2 par2_r2 par3_r2 par4_r2 par5_r2 relpar_r2 exp_r2 ///
timecare_r2 timechore_r2 timepwork_r2 timenpwork_r2 timeschool_r2 timestudy_r2 timeleisure_r2 chwork_r2 exp_cloth_r2 exp_school_r2 ///
trust_index_r2 pride_index_r2 agency_index_r2 respect_index_r2 rscore_ppvt_r2 rscore_math_r2 score_ppvt_r2 score_math_r2 write_r2 grado_r2 ///
gu_liveclose gu_financesiblings gu_helphousework gu_financeparents gu_caresiblings gu_careparents gu_emotsupport guexpectations credit_r2 ///
age_earnmoney age_leaveeduc age_financindep age_leavehh age_married age_child age_earnmoney_child age_leaveeduc_child age_financindep_child age_leavehh_child age_married_child

save "working/r2_intermedia.dta", replace

***** RONDA 3 *****

use "input/peru_r3/olderchild/pe_oc_householdmemberlevel.dta", clear
gen cohort="oc"
append using "input/peru_r3/youngerchild/pe_yc_householdmemberlevel.dta"
replace cohort="yc" if cohort==""
merge m:1 CHILDID using "$base/input/peru_r3/olderchild/pe_oc_householdlevel.dta", nogen keepus(PLACEID)
merge m:1 CHILDID using "$base/input/peru_r3/youngerchild/pe_yc_householdlevel.dta", nogen keepus(PLACEID) update

keep if PLACEID!=""

preserve

keep if AGE<=17

recode SLEEPR3 CHCARER3 HHCHRER3 NPYWRKR3 PAYWRKR3 SCHOOLR3 STUDYR3 PLAYR3 (77 99 = .)
egen totalhours=rowtotal(SLEEPR3 CHCARER3 HHCHRER3 NPYWRKR3 PAYWRKR3 SCHOOLR3 STUDYR3 PLAYR3)

gen hours_study_m=(CHCARER3+HHCHRER3)/totalhours if MEMSEX==1&DISABR3!=5
gen hours_work_m=(NPYWRKR3+PAYWRKR3)/totalhours if MEMSEX==1&DISABR3!=5
gen hours_chores_m=(STUDYR3+SCHOOLR3)/totalhours if MEMSEX==1&DISABR3!=5

gen hours_study_f=(CHCARER3+HHCHRER3)/totalhours if MEMSEX==2&DISABR3!=5
gen hours_work_f=(NPYWRKR3+PAYWRKR3)/totalhours if MEMSEX==2&DISABR3!=5
gen hours_chores_f=(STUDYR3+SCHOOLR3)/totalhours if MEMSEX==2&DISABR3!=5

gen educinv_m=STUDYR3/totalhours if MEMSEX==1
gen educinv_f=STUDYR3/totalhours if MEMSEX==2

gen m_hours=(MEMSEX==1)
replace m_hours=. if MEMSEX==88
gen f_hours=(MEMSEX==2)
replace f_hours=. if MEMSEX==88

collapse (mean) hours_study_m hours_study_f hours_work_m hours_work_f hours_chores_m hours_chores_f educinv_m educinv_f (sum) m_hours f_hours, by(PLACEID)
drop if PLACEID=="PE88C88"

save "working/hours_r3", replace

restore

keep if AGE>17

gen commsup_m=1 if (GRADER3==14|GRADER3==16|GRADER3==19|(GRADER3==13&(STILLR3==1|STILLR3==2))|(GRADER3==15&(STILLR3==1|STILLR3==2)))&MEMSEX==1
replace commsup_m=0 if commsup_m==.&MEMSEX==1
gen commsup_f=1 if (GRADER3==14|GRADER3==16|GRADER3==19|(GRADER3==13&(STILLR3==1|STILLR3==2))|(GRADER3==15&(STILLR3==1|STILLR3==2)))&MEMSEX==2
replace commsup_f=0 if commsup_f==.&MEMSEX==2

gen commsupy_m=1 if (GRADER3==14|GRADER3==16|GRADER3==19|(GRADER3==13&(STILLR3==1|STILLR3==2))|(GRADER3==15&(STILLR3==1|STILLR3==2)))&MEMSEX==1&AGE<=35
replace commsupy_m=0 if commsupy_m==.&MEMSEX==1&AGE<=35
gen commsupy_f=1 if (GRADER3==14|GRADER3==16|GRADER3==19|(GRADER3==13&(STILLR3==1|STILLR3==2))|(GRADER3==15&(STILLR3==1|STILLR3==2)))&MEMSEX==2&AGE<=35
replace commsupy_f=0 if commsupy_f==.&MEMSEX==2&AGE<=35

gen m_sup=(MEMSEX==1)
replace m_sup=. if MEMSEX==.
gen f_sup=(MEMSEX==2)
replace f_sup=. if MEMSEX==.

gen m_supy=(MEMSEX==1)&AGE<=35
replace m_supy=. if MEMSEX==.
gen f_supy=(MEMSEX==2)&AGE<=35
replace f_supy=. if MEMSEX==.

collapse (mean) commsup_m commsup_f commsupy_m commsupy_f (sum) m_sup f_sup m_supy f_supy, by(PLACEID)
drop if PLACEID=="PE88C88"

save "working/commsup_r3", replace

***

use "input/peru_r2/pesubhouseholdmember12.dta", clear
ds CHILDID ID, not

foreach var in `r(varlist)' {
	ren `var' `var'_r2
}

tempfile roster_r2
save `roster_r2'

use "input/peru_r3/olderchild/pe_oc_householdmemberlevel.dta", clear

merge 1:1 CHILDID ID using `roster_r2'

keep CHILDID ID MEMSEX* RELATE* LIVHSE* _merge
keep if ((RELATE>=7&12>=RELATE)&inlist(LIVHSE,1,2))|((RELATE_r2>=7&12>=RELATE_r2)&inlist(LIVHSE_r2,1,2))

foreach var in MEMSEX RELATE {
	replace `var' = `var'_r2 if _merge == 2
	replace `var' = `var'_r2 if LIVHSE == 2 & _merge == 3
}

replace LIVHSE = 2 if _merge == 2
drop if LIVHSE == 3

gen msib_r3=(MEMSEX==1)&LIVHSE==1
gen fsib_r3=(MEMSEX==2)&LIVHSE==1

gen msib_all_r3=(MEMSEX==1)
gen fsib_all_r3=(MEMSEX==2)

collapse (sum) msib_r3 fsib_r3 msib_all_r3 fsib_all_r3, by(CHILDID)

merge 1:1 CHILDID using "$base/input/peru_r3/olderchild/pe_oc_householdlevel.dta", nogen

merge m:1 PLACEID using "working/hours_r3", nogen
merge m:1 PLACEID using "working/commsup_r3", nogen

gen creditcons=(OBTNLNR3==0)
replace creditcons=. if OBTNLNR3==79
replace creditcons=0 if NOASKLR3==1

gen creditcons2=(OBTNLNR3==0)
replace creditcons=. if OBTNLNR3==79
replace creditcons2=1 if GETLNR3==0
replace creditcons=0 if NOASKLR3==1

gen riskaverse=AGREER33==2
replace riskaverse=. if AGREER33==77|AGREER33==79|AGREER33==99

gen safestreet=R3CTR6>2 if R3CTR6!=77

ren (wi HHSIZE MVDTYPR3 SEEDADR3 agechild CAREIDR3 TYSCR309 BOOKHMR3 ENREADR3 ///
NMFRNDR3 AFTRSCR3 PRNTFRR3 TCHRR3 CLSRLTR3) ///
(wi_r3 hhsize_r3 moved_r3 seedad_r3 agechild_r3 careid_r3 sctype_r3 booksathome encouragetoread ///
parcontrol1 parcontrol2 parcontrol3 parcontrol4 closerel)

destring TYPESITE, replace
gen rural_r3=(TYPESITE==2)
replace rural_r3=. if TYPESITE==.

gen costa_r3=(REGION==31)
gen sierra_r3=(REGION==32)
gen selva_r3=(REGION==33)
recode costa_r3 sierra_r3 selva_r3 (88=.)

recode sctype_r3 (5 8 88 = .) (7 10 = 1) (9 = 0)
recode seedad_r3 (7=3) (88=.)
recode encouragetoread (3=1) (1=3)

recode GRDER309 GRDER308 GRDER307 GRDER306 GRDER305 GRDER304 GRDER303 GRDER302 GRDER301 GRDER300 GRDER399 (88 99 = 0)
egen grado_r3=rowmax(GRDER309 GRDER308 GRDER307 GRDER306 GRDER305 GRDER304 GRDER303 GRDER302 GRDER301 GRDER300 GRDER399)

replace grado_r3=GRDER308 if grado_r3==88
replace grado_r3=GRDER307 if grado_r3==88
replace grado_r3=GRDER306 if grado_r3==88

replace careid_r3=MUMIDR3 if PRIMUMR3==1
replace careid_r3=DADIDR3 if PRIDADR3==1

drop if CHILDID==""

merge 1:m CHILDID using "input/peru_r3/olderchild/pe_oc_householdmemberlevel.dta", nogen keepus(ID GRADER3)
gen x=(ID==careid_r3)
egen y=max(x), by(CHILDID)
egen z=tag(CHILDID)
drop if ID!=careid_r3&y==1
drop if y==0&z==0
ren GRADER3 careed_r3
recode careed_r3 (0 1 2 3 4 5 = 0) (6 7 8 9 10 = 1) (11 13 15 = 2) (14 16 19 = 3) (17 18 . = .)
replace careed_r3=. if y==0

merge 1:1 CHILDID using "$base/input/peru_r3/olderchild/pe_oc_childlevel.dta"
drop _merge

ren LVLEDCR3 exp_r3
recode exp_r3 (4 6 8 11 17 = 0) (14 16 19 = 1) (79 88 = .)

recode SEXSCHR3 SEXMEDR3 SEXFRNR3 SEXNRSR3 INFSEXR3 (77 79 88 99 = .)
gen sexinfo=(SEXSCHR3==1| SEXMEDR3==1| SEXFRNR3==1| SEXNRSR3==1| INFSEXR3==1)
ren SEXSCHR3 sexinfo_school

recode PRGFRSR3 WSHAFTR3 USECNDR3 LKSHLTR3 HIVSEXR3 (77 79 88 99 = .)
gen sexknow_1=(PRGFRSR3==2)
gen sexknow_2=(WSHAFTR3==2)
gen sexknow_3=(USECNDR3==1)
gen sexknow_4=(LKSHLTR3==2)
gen sexknow_5=(HIVSEXR3==1)
egen sexknow=rowtotal(sexknow_1 sexknow_2 sexknow_3 sexknow_4 sexknow_5)

gen chwork_r3=(5>=MNPDACR3)
replace chwork_r3=. if MNPDACR3==79

ren (SLEEPR3 CROTHR3 DMTSKR3 TSFARMR3 ACTPAYR3 ATSCHR3 STUDYGR3 LSURER3 ENRSCHR3 SPVIEWR3 TRFAIRR3 CMSITGR3 CMBRTGR3 CMBRFRR3 CMSIFRR3 BFAMLYR3 zhfa zwfa bmi MISSCHR3 /* TCHMSSR3 TCHLTER3 TCHINFR3 QLTSCHR3*/) ///
	(timesleep_r3 timecare_r3 timechore_r3 timedomwork_r3 timepwork_r3 timeschool_r3 timestudy_r3 timeleisure_r3 enrschool_r3 par1_r3 par2_r3 par3_r3 par4_r3 par5_r3 par6_r3 domesticviolence zhfa_r3 zwfa_r3 bmi_r3 falta_r3 /*teacher1_r3 teacher2_r3 teacher3_r3 teachqual_r3*/)
	
recode timesleep_r3 timecare_r3 timechore_r3 timedomwork_r3 timepwork_r3 timeschool_r3 timestudy_r3 timeleisure_r3 enrschool_r3 (77 79 88 = .)

egen time_workathome_r3 = rowtotal(timecare_r3 timechore_r3 timedomwork_r3)
	
gen ob_r3= (zwfa_r3>2 & zwfa_r3<=5)

recode par1_r3 par2_r3 (77 79 88 99 = .) (1 = 2) (2 = 1) (3 = 0)
recode par3_r3 par4_r3 par5_r3 (77 79 88 99 = .) (2 = 1) (1 = 0) (3 = 2)

egen relpar_r3=rowmean(par1_r3 par2_r3 par3_r3 par4_r3 par5_r3 par6_r3)
egen n_relpar=rownonmiss(par1_r3 par2_r3 par3_r3 par4_r3 par5_r3 par6_r3)
replace relpar_r3=. if n_relpar<=2
drop n_relpar 

recode domesticviolence (79 88 99 = .)

recode falta_r3 (79 88 = . )
recode timesleep_r3 timecare_r3 timechore_r3 timepwork_r3 timeschool_r3 timestudy_r3 timeleisure_r3 enrschool_r3 (79 88 99 = . )

gen safestreet_child=CSFEOWR3>2
replace safestreet_child = . if CSFEOWR3 == 77 | CSFEOWR3 == 79 | CSFEOWR3 == 99

* Expenditure

ren (SPYRR303 SPYRR304 SPYRR311 SPYRR312 SPYRR313 SPYRR315 SPYRR316 SPYR316B) (exp_cloth1_r3 exp_cloth2_r3 exp_school1_r3 exp_school2_r3 exp_school3_r3 exp_school4_r3 exp_school5_r3 exp_school6_r3)
recode exp_cloth1_r3 exp_cloth2_r3 exp_school1_r3 exp_school2_r3 exp_school3_r3 exp_school4_r3 exp_school5_r3 exp_school6_r3 (-77 -88 = .)

gen sib_r3=msib_r3+fsib_r3
replace sib_r3=0 if sib_r3==.
gen children_r3=sib_r3+1

gen sib_all_r3=msib_all_r3+fsib_all_r3
replace sib_all_r3=0 if sib_all_r3==.
gen children_all_r3=sib_all_r3+1

gen male_children_all_r3 = msib_all_r3 

replace male_children_all_r3 = male_children_all_r3 + 1 if sex == 1 
gen male_rate_r3 = male_children_all_r3 / children_all_r3
replace male_rate_r3 = 0 if children_all_r3 == 1 & sex == 2
replace male_rate_r3 = 1 if children_all_r3 == 1 & sex == 1

replace male_rate_r3 = . if wi_r3 == .

gen female_children_all_r3 = fsib_all_r3 

replace female_children_all_r3 = female_children_all_r3 + 1 if sex == 2 
gen female_rate_r3 = female_children_all_r3 / children_all_r3
replace female_rate_r3 = 0 if children_all_r3 == 1 & sex == 1
replace female_rate_r3 = 1 if children_all_r3 == 1 & sex == 2

replace female_rate_r3 = . if wi_r3 == .

egen exp_cloth_r3=rowtotal(exp_cloth1_r3 exp_cloth2_r3)
replace exp_cloth_r3=exp_cloth_r3/children_r3
replace exp_cloth_r3=. if Situac_R3!=1
egen exp_school_r3=rowtotal(exp_school1_r3 exp_school2_r3 exp_school3_r3 exp_school4_r3 exp_school5_r3 exp_school6_r3)
replace exp_school_r3=exp_school_r3/children_r3
replace exp_school_r3=. if Situac_R3!=1

*** Skills

* Cognitive
gen ppvt_spanish_r3= (ppvtlang2==1)
gen score_ppvt_r3=ppvt
gen score_math_r3=math
gen score_cloze_r3=cloze

gen rscore_ppvt_r3=rppvt_co
gen rscore_math_r3=rmath_co
gen rscore_cloze_r3=rcloze_co

* Trust index
recode CTRUSTR3 CSFEOWR3 CGOVRGR3 (77 79 88 99 = .)
ren (CTRUSTR3 CSFEOWR3 CGOVRGR3) (tr1_r3 tr2_r3 tr3_r3)

* Self-respect index
recode CSHPRSR3 CTRTWSR3 CTRTRSR3 CTESMER3(77 79 88 99 = .)
ren (CSHPRSR3 CTRTWSR3 CTRTRSR3 CTESMER3) (sd1_r3 sd2_r3 sd3_r3 sd4_r3)

* Pride index
recode CASHSHR3 CASHCLR3 CEMBBKR3 CWRUNIR3 CASHWKR3 CCLTRGR3 (77 79 88 99 = .)
ren (CASHSHR3 CASHCLR3 CEMBBKR3 CWRUNIR3 CASHWKR3 CCLTRGR3) (ps1_r3 ps2_r3 ps3_r3 ps4_r3 ps5_r3 ps6_r3)

* Agency index
recode CTRYHDR3 CPLDECR3 CFTRWRR3 CNOCHCR3 CBRJOBR3(77 79 88 99 = .)

foreach x in CPLDECR3 CNOCHCR3 {
gen `x'_inv=.
replace `x'_inv=1 if `x'==5
replace `x'_inv=2 if `x'==4
replace `x'_inv=3 if `x'==3
replace `x'_inv=4 if `x'==2
replace `x'_inv=5 if `x'==1
drop `x'
rename `x'_inv `x'
}

ren (CTRYHDR3 CPLDECR3 CFTRWRR3 CNOCHCR3 CBRJOBR3) (ag1_r3 ag2_r3 ag3_r3 ag4_r3 ag5_r3)

* Standardizing

egen total_r3_tr=rowtotal(tr1_r3 tr2_r3 tr3_r3)
egen nonmiss_r3_tr=rownonmiss(tr1_r3 tr2_r3 tr3_r3)
gen trust_index_r3=total_r3_tr/nonmiss_r3_tr

egen total_r3_ps=rowtotal(ps1_r3 ps2_r3 ps3_r3 ps4_r3 ps5_r3)
egen nonmiss_r3_ps=rownonmiss(ps1_r3 ps2_r3 ps3_r3 ps4_r3 ps5_r3)
gen pride_index_r3=total_r3_ps/nonmiss_r3_ps

egen total_r3_ag=rowtotal(ag1_r3 ag2_r3 ag3_r3 ag4_r3 ag5_r3)
egen nonmiss_r3_ag=rownonmiss(ag1_r3 ag2_r3 ag3_r3 ag4_r3 ag5_r3)
gen agency_index_r3=total_r3_ag/nonmiss_r3_ag

egen total_r3_sd=rowtotal(sd1_r3 sd2_r3 sd3_r3 sd4_r3)
egen nonmiss_r3_sd=rownonmiss(sd1_r3 sd2_r3 sd3_r3 sd4_r3)
gen respect_index_r3=total_r3_sd/nonmiss_r3_sd

gen domwork_r3 = timechore_r3 + timecare_r3 + timedomwork_r3

foreach v of varlist score_ppvt_r3 score_math_r3 score_cloze_r3 rscore_ppvt_r3 rscore_math_r3 rscore_cloze_r3 trust_index_r3 pride_index_r3 agency_index_r3 respect_index_r3 relpar_r3 timestudy_r3 timechore_r3 chwork_r3 exp_cloth_r3 exp_school_r3 {
egen mean_`v' = mean(`v')
egen sd_`v'=sd(`v')
gen z_`v'= (`v' - mean_`v')/sd_`v'
drop `v'
rename z_`v' `v'
}

gen CHILDCODE=substr(CHILDID,3,.)
destring CHILDCODE, replace

keep CHILDCODE CHILDID PLACEID PLACENR3 wi_r3 hhsize_r3 moved_r3 seedad_r3 rural_r3 costa_r3 sierra_r3 selva_r3 chwork_r3 sctype_r3 ///
sib_r3 sib_all_r3 msib_r3 msib_all_r3 fsib_r3 fsib_all_r3 children_r3 children_all_r3 male_children_all_r3 male_rate_r3 ///
exp_r3 creditcons creditcons2 riskaverse safestreet safestreet_child ///
time_workathome_r3 timesleep_r3 timecare_r3 timechore_r3 timedomwork_r3 timepwork_r3 timeschool_r3 timestudy_r3 timeleisure_r3 domwork_r3 exp_cloth_r3 exp_school_r3 enrschool_r3 par1_r3 par2_r3 par3_r3 par4_r3 par5_r3 relpar_r3 zhfa_r3 zwfa_r3 bmi_r3 ob_r3 ///
trust_index_r3 pride_index_r3 agency_index_r3 respect_index_r3 score_ppvt_r3 score_math_r3 score_cloze_r3 rscore_ppvt_r3 rscore_math_r3 rscore_cloze_r3 agechild_r3 careed_r3 grado_r3 sexinfo sexinfo_school sexknow /* teacher1_r3 teacher2_r3 teacher3_r3 teachqual_r3 */ ///
hours_study_m hours_study_f hours_work_m hours_work_f hours_chores_m hours_chores_f educinv_m educinv_f ///
commsup_m commsup_f commsupy_m commsupy_f m_sup f_sup m_supy f_supy

save "working/r3_intermedia.dta", replace

*** Constructed

use "$base/input/constructed/peru_constructed.dta", replace

keep if yc==0
keep childid round totalexp_rpc foodexp_rpc nfoodexp_rpc headedu caresex caredu momedu dadedu hhsize zbfa zhfa panel1234
recode headedu caredu momedu dadedu (0 1 2 3 4 5 20 = 0) (6 7 8 9 10 = 1) (11 13 15 21 = 2) (14 16 19 22 = 3) (17 18 77 . = .)

ren (totalexp_rpc foodexp_rpc nfoodexp_rpc headedu caresex caredu momedu dadedu hhsize zbfa zhfa) (totalexppc_r foodexppc_r nfoodexppc_r headed_r caresex_r careedcons_r mothed_r fathed_r hhsizecons_r zbfa_r zhfa_r)

reshape wide totalexppc_r foodexppc_r nfoodexppc_r headed_r mothed_r fathed_r hhsizecons_r caresex_r careedcons_r zbfa_r zhfa_r panel1234, i(childid) j(round)

drop totalexppc_r1
drop totalexppc_r4

replace totalexppc_r2=ln(totalexppc_r2)
replace totalexppc_r3=ln(totalexppc_r3)

ren panel12341 panel1234
drop panel12342 panel12343 panel12344
gen CHILDCODE=substr(childid,3,.)
destring CHILDCODE, replace
drop childid

gen careed=mothed_r1
replace careed=careedcons_r1 if mothed_r1==.
replace careed=mothed_r2 if careed==.
replace careed=careedcons_r2 if careed==.
replace careed=mothed_r3 if careed==.
replace careed=careedcons_r3 if careed==.

gen careed_prim=(careed==1|careed==2|careed==3)
replace careed_prim=. if careed==.
gen careed_sec=(careed==2|careed==3)
replace careed_sec=. if careed==.
gen careed_ter=(careed==3)
replace careed_ter=. if careed==.

replace mothed_r1=careedcons_r1 if mothed_r1==.&caresex_r1==2
replace fathed_r1=careedcons_r1 if fathed_r1==.&caresex_r1==1

gen mothed_prim=(mothed_r1==1|mothed_r1==2|mothed_r3==3)
replace mothed_prim=. if mothed_r1==.
gen mothed_sec=(mothed_r1==2|mothed_r3==3)
replace mothed_sec=. if mothed_r1==.
gen mothed_ter=(mothed_r3==3)
replace mothed_ter=. if mothed_r1==.

gen fathed_prim=(fathed_r1==1|fathed_r1==2|fathed_r3==3)
replace fathed_prim=. if fathed_r1==.
gen fathed_sec=(fathed_r1==2|fathed_r3==3)
replace fathed_sec=. if fathed_r1==.
gen fathed_ter=(fathed_r3==3)
replace fathed_ter=. if fathed_r1==.

save "working/constructed_intermedia.dta", replace

*** Ronda 4

********* E. Superior

use "$base/input/peru_r4/ch_oc/pe_r4_occh_educationhistoryindexchild.dta"

gen a=(EDCHSTR4==13&(11==GRDER4|10==GRDER4|9==GRDER4))
egen still_school=max(a), by(CHILDCODE) /* se considera que no terminará el colegio si a los 19 años está en un grado menor a 3 de secundaria */

gen b=(EDCHSTR4==12&11==GRDER4)
egen sabat=max(b), by(CHILDCODE)

gen c=(11==GRDER4&EDCHSTR4!=13)
egen finish_school=max(c), by(CHILDCODE) /* se asume que terminaron el colegio los que cursaron el último grado al menos un año antes de la encuesta */

gen d=(GRDER4==13|GRDER4==14|GRDER4==15|GRDER4==16|GRDER4==21|GRDER4==22) /* inició algún programa de educación superior */
egen enroll_sup=max(d), by(CHILDCODE) 
gen enroll_sup2 = enroll_sup
replace enroll_sup=. if still_school==1 /* se asume que es posible que niños en los últimos tres años de secundaria ingresen a educ. superior en el futuro */

gen enroll_sup1=enroll_sup
replace enroll_sup1=. if enroll_sup==0&sabat==1

gen e=(GRDER4==13|GRDER4==14|GRDER4==21|GRDER4==22) /* inició algún programa de educación superior no universitaria */
replace e=2 if GRDER4==15|GRDER4==16 /* inició algún programa de educación superior universitaria */
egen enroll_sup_det=max(e), by(CHILDCODE) /* 1 para los que iniciarion no univ, 2 para los que iniciaron univ. */
replace enroll_sup_det=. if still_school==1 /* se asume que es posible que niños en los últimos tres años de secundaria ingresen a educ. superior en el futuro */

gen enroll_sup_det1=enroll_sup_det
replace enroll_sup_det1=. if enroll_sup_det==0&sabat==1

gen f=(EDCHSTR4==13&(GRDER4==13|GRDER4==14|GRDER4==15|GRDER4==16|GRDER4==21|GRDER4==22)) /* enrolled en educ superior en el 2013 */
replace f=1 if GRDER4==14|GRDER4==16|GRDER4==22 /* terminó algún programa de educ superior */
egen still_sup=max(f), by(CHILDCODE)
gen still_sup2 = still_sup
replace still_sup=. if still_school==1 /* se asume que es posible que niños en los últimos tres años de secundaria ingresen a educ. superior en el futuro */

gen still_sup1=still_sup
replace still_sup1=. if still_sup==0&sabat==1

gen g=(EDCHSTR4==13&(GRDER4==13|GRDER4==14|GRDER4==21|GRDER4==22)) /* enrolled en educ superior no universitaria en el 2013 */
replace g=1 if GRDER4==14|GRDER4==22 /* terminó algún programa de educ superior no universitaria */
replace g=2 if EDCHSTR4==13&GRDER4==15|GRDER4==16 /* enrolled en educ superior universitaria en el 2013 */
replace g=2 if GRDER4==16 /* terminó algún programa de educ superior universitaria */
egen still_sup_det=max(g), by(CHILDCODE)
replace still_sup_det=. if still_school==1 /* se asume que es posible que niños en los últimos tres años de secundaria ingresen a educ. superior en el futuro */

gen still_sup_det1=still_sup_det
replace still_sup_det1=. if still_sup_det==0&sabat==1

** public vs private

gen h=TYSCR4==1 if (GRDER4==13|GRDER4==14|GRDER4==15|GRDER4==16|GRDER4==21|GRDER4==22) & !missing(TYSCR4) /* inició algún programa de educación superior privada */
egen type_sup = max(h), by(CHILDCODE)

** migration for higher ed

gen i = DEPTR4 if (GRDER4==13|GRDER4==14|GRDER4==15|GRDER4==16|GRDER4==21|GRDER4==22)
egen dept_sup = max(i), by(CHILDCODE) // no instances of children moving to a different sup institution

drop a b c d e f g h i

collapse (max) enroll_sup enroll_sup1 enroll_sup2 enroll_sup_det enroll_sup_det1 ///
	still_sup still_sup1 still_sup2 still_sup_det still_sup_det1 finish_school still_school type_sup dept_sup ///
	, by(CHILDCODE)

merge 1:1 CHILDCODE using "$base/input/peru_r4/ch_oc/pe_r4_occh_olderchild.dta"
drop _merge

gen agechild_r4=agemon

recode GGVBRTR4 (-79 = .)
egen children=rowtotal(BHVCHDR4 GGVBRTR4)
replace children=. if BHVCHDR4==.& GGVBRTR4==.
replace children=1 if children==2

gen cohab=(MRTSTSR4==1|MRTSTSR4==3)

merge 1:1 CHILDCODE using "input/peru_r4/hh_oc/pe_r4_ochh_olderhousehold.dta"

ren wi wi_r4
gen rural_r4=(RURICKR4==0|RURICKR4==1)
replace rural_r4=. if RURICKR4==.

ren (BECAAPPR4 BECABENR4 PAYSNGR4) (applied_beca18_r4 received_beca18_r4 scholarship_r4)

keep agechild_r4 children cohab wi_r4 CHILDCODE enroll_sup enroll_sup1 enroll_sup2 ///
	enroll_sup_det enroll_sup_det1 still_sup still_sup1 still_sup2 still_sup_det still_sup_det1 ///
	finish_school still_school type_sup dept_sup ///
	applied_beca18_r4 received_beca18_r4 scholarship_r4

***** Juntando bases

merge 1:1 CHILDCODE using "working/r3_intermedia.dta", gen(m_r3)

merge 1:1 CHILDCODE using "working/r2_intermedia.dta", gen(m_r2)

merge 1:1 CHILDCODE using "working/r1_intermedia.dta", gen(m_r1)

merge 1:1 CHILDCODE using "working/constructed_intermedia.dta", gen(m_cons)

drop if m_cons==2
drop m_cons

ren (CHILDCODE CHILDID) (childcode childid)
 
factor write_r1 numeracy_r1 read_r1 raven_r1, pcf factors(1)
rotate
predict skills_c_r1

factor write_r1 numeracy_r1 read_r1 raven_r1, factors(1)
rotate
predict skills_c_r1_efa

factor chwork_r1 chores_r1 seedad_r1, pcf factors(1)
rotate 
predict parinv_r1

factor chwork_r1 chores_r1 seedad_r1, factors(1)
rotate 
predict parinv_r1_efa

replace parinv_r1=-parinv_r1
replace parinv_r1_efa=-parinv_r1_efa

gen parinv_r1_bysex = .
forvalues s = 0/1 {
	
	factor chwork_r1 chores_r1 seedad_r1 if sex == `s', pcf factors(1)
	rotate 
	predict parinv_r1_s`s' if sex == `s'
	replace parinv_r1_bysex = parinv_r1_s`s' if sex == `s'
	drop parinv_r1_s`s'
	
}
replace parinv_r1_bysex = -parinv_r1_bysex

factor score_ppvt_r2 score_math_r2 write_r2, pcf factors(1)
rotate
predict skills_c_r2

factor rscore_ppvt_r2 rscore_math_r2 write_r2, pcf factors(1)
rotate
predict skills_cr_r2

factor rscore_ppvt_r2 rscore_math_r2 write_r2, factors(1)
rotate
predict skills_cr_r2_efa

factor pride_index_r2 agency_index_r2 respect_index_r2, pcf factors(1)
rotate
predict skills_nc_r2

factor pride_index_r2 agency_index_r2, pcf factors(1)
rotate
predict skills_nc1_r2

factor pride_index_r2 agency_index_r2 respect_index_r2 trust_index_r2, pcf factors(1)
rotate
predict skills_nc2_r2 

factor pride_index_r2 agency_index_r2 respect_index_r2 trust_index_r2, factors(1)
rotate
predict skills_nc2_r2_efa

factor relpar_r2 timestudy_r2 timechore_r2 chwork_r2 exp_cloth_r2 exp_school_r2, pcf factors(1)
rotate
predict parinv_r2

factor relpar_r2 timestudy_r2 timechore_r2 chwork_r2 exp_cloth_r2 exp_school_r2, factors(1)
rotate
predict parinv_r2_efa

gen parinv_r2_bysex = .
forvalues s = 0/1 {
	
	factor relpar_r2 timestudy_r2 timechore_r2 chwork_r2 exp_cloth_r2 exp_school_r2 if sex == `s', pcf factors(1)
	rotate 
	predict parinv_r2_s`s' if sex == `s'
	replace parinv_r2_bysex = parinv_r2_s`s' if sex == `s'
	drop parinv_r2_s`s'
	
}

factor score_ppvt_r3 score_math_r3 score_cloze_r3, pcf factors(1)
rotate
predict skills_c_r3

factor rscore_ppvt_r3 rscore_math_r3 rscore_cloze_r3, pcf factors(1)
rotate
predict skills_cr_r3

factor rscore_ppvt_r3 rscore_math_r3 rscore_cloze_r3, factors(1)
rotate
predict skills_cr_r3_efa

factor pride_index_r3 agency_index_r3 respect_index_r3, pcf factors(1)
rotate
predict skills_nc_r3 

factor pride_index_r3 agency_index_r3, pcf factors(1)
rotate
predict skills_nc1_r3 

factor pride_index_r3 agency_index_r3 respect_index_r3 trust_index_r3, pcf factors(1)
rotate
predict skills_nc2_r3 

factor pride_index_r3 agency_index_r3 respect_index_r3 trust_index_r3, factors(1)
rotate
predict skills_nc2_r3_efa 

gen sib_r1=msib_r1+fsib_r1
replace sib_r1=0 if sib_r1==.

ren CLUSTID clustid

recode sex (0=1) (1=0)

foreach r in 1 2 3{
egen mean_wi`r' = mean(wi_r`r')
egen sd_wi`r'=sd(wi_r`r')
gen wi_r`r'_sd= (wi_r`r' - mean_wi`r')/sd_wi`r'
xtile terwi_r`r'=wi_r`r', nq(3)
foreach ter in 1 2 3{
gen terwi`ter'_r`r'=(terwi_r`r'==`ter')
}
}

label drop SEX
label define sex 0 "male" 1 "female"
label var sex sex

gen order_1=(order>=1)
gen order_2=(order>=2)

replace msib_r2=0 if msib_r2==.&wi_r2!=.
replace fsib_r2=0 if fsib_r2==.&wi_r2!=.
replace sib_r2=. if wi_r2==.&msib_r2==.&fsib_r2==.

replace msib_all_r2=0 if msib_all_r2==.&wi_r2!=.
replace fsib_all_r2=0 if fsib_all_r2==.&wi_r2!=.

replace msib_r3=0 if msib_r3==.&wi_r2!=.
replace fsib_r3=0 if fsib_r3==.&wi_r2!=.
replace sib_r3=. if wi_r3==.&msib_r3==.&fsib_r3==.

replace msib_all_r3=0 if msib_all_r3==.&wi_r2!=.
replace fsib_all_r3=0 if fsib_all_r3==.&wi_r2!=.

gen msib_younger_r3 = msib_all_r3 - msib_older
replace msib_younger_r3 = 0 if msib_younger_r3 < 0
gen fsib_younger_r3 = fsib_all_r3 - fsib_older
replace fsib_younger_r3 = 0 if fsib_younger_r3 < 0

****

replace PLACEID=PLACENR3 if PLACEID=="PE88C88"&PLACENR3!="PE88C88"

save "output/panel_final", replace

use "input/peru_r3\pe_r3_community_comm_level_r3.dta"

ren (ROBBERY CTTLTHFT PRSTITN GANGS ADLTBNDS DRUGADCT ALCHLSM VLNTCRME) ///
	(robbery_r3 cattletheft_r3 prostitution_r3 gangs_r3 adultbands_r3 drugaddiction_r3 alcoholism_r3 violentcrime_r3)

recode robbery_r3 cattletheft_r3 prostitution_r3 gangs_r3 adultbands_r3 drugaddiction_r3 alcoholism_r3 violentcrime_r3 (99 = .)

foreach var in robbery_r3 cattletheft_r3 gangs_r3 adultbands_r3 violentcrime_r3 {
    qui summ `var'
	replace `var' = (`var' - `r(mean)') / `r(sd)'	
}

egen crime_index_r3 = rowmean(robbery_r3 cattletheft_r3 gangs_r3 adultbands_r3 violentcrime_r3)

qui summ crime_index_r3 
replace crime_index_r3 = (crime_index_r3 - `r(mean)') / `r(sd)'

rename (PSTTCHTM UNVTYTIM) (univ_r3 techinst_r3)
recode univ_r3 techinst_r3 (-77 -88 = .)

gen educdist_r3=univ_r3 if univ_r3!=.
replace educdist_r3=techinst_r3 if (techinst_r3!=.&univ_r3==.)|(techinst_r3!=.&univ_r3!=.&techinst_r3<univ_r3)

replace educdist_r3 = educdist_r3 / 60

gen univnear_r3=(univ_r3<=60)
gen univnear1_r3=(univ_r3<=45)
gen univnear2_r3=(univ_r3<=90)

gen techinstnear_r3=(techinst_r3<=60)
gen techinstnear1_r3=(techinst_r3<=45)
gen techinstnear2_r3=(techinst_r3<=90)

gen educnear_r3=(univnear_r3==1|techinstnear_r3==1)
gen educnear1_r3=(univnear1_r3==1|techinstnear1_r3==1)
gen educnear2_r3=(univnear2_r3==1|techinstnear2_r3==1)

gen educfar2_r3=(educnear2_r3==0)
gen educfar1_r3=(educnear1_r3==0)
gen educfar_r3=(educnear_r3==0)

replace educfar_r3=. if univ_r3==.&techinst_r3==.
replace educfar1_r3=. if univ_r3==.&techinst_r3==.
replace educfar2_r3=. if univ_r3==.&techinst_r3==.

ren departid departid_r3 

tab departid_r3, gen(dep)
ren (dep? dep??) (dep?_r3 dep??_r3)
drop dep8_r3 // only one obs

keep PLACEID departid_r3 dep*_r3 clustid educdist_r3 univnear_r3 univnear1_r3 univnear2_r3 techinstnear_r3 techinstnear1_r3 techinstnear2_r3 educnear_r3 educnear1_r3 educnear2_r3 educfar_r3 educfar1_r3 educfar2_r3 ///
	robbery_r3 cattletheft_r3 prostitution_r3 gangs_r3 adultbands_r3 drugaddiction_r3 alcoholism_r3 violentcrime_r3 crime_index_r3

merge 1:m PLACEID using "output/panel_final"
drop if _merge==1

* migration for higher ed:

gen migrated_sup = departid_r3 != dept_sup if !missing(departid_r3) & !missing(dept_sup)
gen migrated_lima_sup = departid_r3 != 15 & dept_sup == 15 if !missing(departid_r3) & !missing(dept_sup)

save "output/panel_final", replace

*===============================================================================